#include "dialogsetting.h"
#include "ui_dialogsetting.h"

#include <unistd.h>

/**
 * @brief 构造函数
 * @param parent
 */
DialogSetting::DialogSetting(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogSetting)
{
    ui->setupUi(this);
//    setAttribute (Qt::WA_DeleteOnClose);
}

/**
 * @brief 析构函数
 */
DialogSetting::~DialogSetting()
{
    qout << "~DialogSetting()";
    delete ui;
}

/**
 * @brief 重载event()函数,主要用来处理翻译事件
 * @param event
 * @return
 */
bool DialogSetting::event(QEvent *event)
{
    //Language changed.
    if(event->type() == QEvent::LanguageChange)
    {
        ui->retranslateUi(this);
    }
    //Must call QWidget:event(event) or the process to be broken
    return QWidget::event(event);
}

/**
 * @brief 重写closeEvent(),并发出对话框关闭信号
 * @param event
 */
void DialogSetting::closeEvent(QCloseEvent */*event*/)
{
    emit setDialogClose();
}

/**
 * @brief 设置用户ID
 * @param userId
 */
void DialogSetting::setUserName(QByteArray userId)
{
    ui->lineEditUserName->setText(userId);
}

/**
 * @brief 设置用户昵称
 * @param userNickName
 */
void DialogSetting::setUserNickName(QByteArray userNickName)
{
    ui->lineEditNickName->setText(userNickName);
}

/**
 * @brief 设置用户组
 * @param groupName
 */
void DialogSetting::setGroupName(QByteArray groupName)
{
    ui->lineEditGroupName->setText(groupName);
}

/**
 * @brief DialogSetting::setStartIp         设置起始ip
 * @param ip  uint IP地址
 */
void DialogSetting::setStartIp(unsigned int ip)
{
    QHostAddress add;
    add.setAddress(ip);
    ui->lineEditStartIp->setText(add.toString());
}

/**
 * @brief DialogSetting::setEndIp           设置结束ip
 * @param ip
 */
void DialogSetting::setEndIp(unsigned int ip)
{
    QHostAddress add;
    add.setAddress(ip);
    ui->lineEditEndIp->setText(add.toString());
}

/**
 * @brief DialogSetting::getUserName        设置用户名
 * @return
 */
QString DialogSetting::getUserName()
{
    return ui->lineEditUserName->text();
}

/**
 * @brief DialogSetting::getUserNickName    获取用户昵称
 * @return
 */
QString DialogSetting::getUserNickName()
{
    return ui->lineEditNickName->text();
}

/**
 * @brief DialogSetting::getGroupName       获取组名
 * @return
 */
QString DialogSetting::getGroupName()
{
    return ui->lineEditGroupName->text();
}

/**
 * @brief DialogSetting::getStartIp         获取起始ip
 * @return
 */
QString DialogSetting::getStartIp()
{
    return ui->lineEditStartIp->text();
}

/**
 * @brief DialogSetting::getEndIp           获取结束ip
 * @return
 */
QString DialogSetting::getEndIp()
{
    return ui->lineEditEndIp->text();
}

/**
 * @brief [slot] 用户名行编辑被编辑
 * @param arg1
 */
void DialogSetting::on_lineEditUserName_textEdited(const QString &arg1)
{
    if(arg1.length()>32  || arg1.length() == 0)
    {
        ui->labelWarning->setText(tr("User name length is from 1-32"));
        return;
    }
    else
    {
        ui->labelWarning->setText("Valid user name");
    }
    return;
}

/**
 * @brief [slot] 昵称行编辑被编辑
 * @param arg1
 */
void DialogSetting::on_lineEditNickName_textEdited(const QString &arg1)
{
    if(arg1.length()>32  || arg1.length() == 0)
    {
        ui->labelWarning->setText(tr("Nick name length is from 1-32"));
        return;
    }
    else
    {
        ui->labelWarning->setText("Valid nick name");
    }
    return;
}

/**
 * @brief [slot] 分组的行编辑被编辑
 * @param arg1
 */
void DialogSetting::on_lineEditGroupName_textEdited(const QString &arg1)
{
    if(arg1.length()>32  || arg1.length() == 0)
    {
        ui->labelWarning->setText(tr("Group name length is from 1-32"));
        return;
    }
    else
    {
        ui->labelWarning->setText("Valid group name");
    }
    return;
}

/**
 * @brief [slot] 起始 IP 的行编辑被编辑
 * @param arg1
 */
void DialogSetting::on_lineEditStartIp_textEdited(const QString &arg1)
{
    QHostAddress tmp;
    if(false == tmp.setAddress(arg1))
    {
        ui->labelWarning->setText(tr("Invalid Start IP Address"));
    }
    else
    {
        ui->labelWarning->setText(tr("Valid Start IP Address"));
    }
    return;
}

/**
 * @brief [slot] 结束 IP 的行编辑被编辑
 * @param arg1
 */
void DialogSetting::on_lineEditEndIp_textEdited(const QString &arg1)
{
    QHostAddress tmp;
    if(false == tmp.setAddress(arg1))
    {
        ui->labelWarning->setText(tr("Invalid End IP Address"));
    }
    else
    {
        ui->labelWarning->setText(tr("Valid End IP Address"));
    }
    return;
}

/**
 * @brief [slot] 接受按钮被点击
 */
void DialogSetting::on_pushButtonAccept_clicked()
{
    QHostAddress start;
    QHostAddress end;
    start.setAddress(ui->lineEditStartIp->text());
    end.setAddress(ui->lineEditEndIp->text());

    if(start.toIPv4Address() > end.toIPv4Address())
    {
        ui->labelWarning->setText(tr("End IP should >= Start IP"));
        return;
    }
    else
    {
        emit setDialogAccepted();
    }
}

/**
 * @brief [slot] 取消按钮被点击
 */
void DialogSetting::on_pushButtonCancel_clicked()
{
    emit  setDialogRejected();
}
